var express = require("express");
var router = express.Router();
const { Category, Course, User } = require("../models");

/* GET home page. */
router.get("/", async (req, res, next) => {
  console.log("envobj", process.env.PORT);

  // 推荐课程 isRecommended 10个 带出user, 分类
  const includeConditions = {
    include: [
      {
        model: User,
        as: "user",
        attributes: ["id", "nickname", "username"],
      },
      {
        model: Category,
        as: "category",
        attributes: ["id", "name"],
      },
    ],
  };
  const recommendedCourse = await Course.findAll({
    order: [["id", "DESC"]],
    where: { isRecommend: true },
    attributes: ["id", "name"],
    offset: 0,
    limit: 10,
    ...includeConditions,
  });

  // 人气课程 点赞
  const hotCourse = await Course.findAll({
    order: [["likeCount", "DESC"]],
    offset: 0,
    limit: 10,
    ...includeConditions,
  });

  // 入门课程
  const introductorCourse = await Course.findAll({
    order: [["id", "DESC"]],
    where: { introductory: true },
    offset: 0,
    limit: 10,
    ...includeConditions,
  });

  res.json({
    code: 200,
    data: {
      recommendedCourse,
      hotCourse,
      introductorCourse,
    },
    msg: "请求成功",
  });
  next();
});

module.exports = router;
